<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>extending javascript </title>
</head>




<body>
extending javascript 
</body>


<script>

// self invoking function 
(function(ns) {
	//console.log('inside an imediate function');
	//console.log(ns);
	extend();
	tmp();
}(this.ns || this.ns || {}));


function tmp() {
	
	var strings = "yay";
	
	//for (i in strings)
		//console.log(i + ":" + strings[i]);
	
	console.log(strings.capitalize());
	
	var tmp = '200px';
	console.log(tmp.pxToInt());
}






function extend() {
	
	/*
	if (!String.prototype.capitalize) {
		String.prototype.capitalize = function() {
			return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
		};
	}
	*/
	
	// hide the capitalize function putting the enumerable to false
	Object.defineProperty(String.prototype, 'capitalize',
	{
		value: function() {
			return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase();
		},
		enumerable: false
	});
	
	
	// Convert css px value like “200px” to an Integer.
	if(!String.prototype.pxToInt)
	{
	    Object.defineProperty(String.prototype, 'pxToInt',
	    {
	        value: function()
	        {
	            return parseInt(this.split('px')[0]);
	        },
	        enumerable: false
	    });
	}
	
	// Checks if string is valid Hex value such as “#CCC” or “#CACACA”.
	if(!String.prototype.isHex)
	{
	    Object.defineProperty(String.prototype, 'isHex',
	    {
	        value: function()
	        {
	            return this.substring(0,1) == '#' &&  
	                   (this.length == 4 || this.length == 7) && 
	                   /^[0-9a-fA-F]+$/.test(this.slice(1));
	        },
	        enumerable: false
	    });
	}
	
	// Reverse a string
	if(!String.prototype.reverse)
	{
	    Object.defineProperty(String.prototype, 'reverse',
	    {
	        value: function()
	        {
	            return this.split( '' ).reverse().join( '' );
	        },
	        enumerable: false
	    });
	}
	
	// Count the number of words in a given string, words being separated by spaces.
	if(!String.prototype.wordCount)
	{
	    Object.defineProperty(String.prototype, 'wordCount',
	    {
	        value: function()
	        {
	            return this.split(' ').length;
	        },
	        enumerable: false
	    });
	}
	
	if(!String.prototype.trim)
	{
	    Object.defineProperty(String.prototype, 'trim',
	    {
	        value: function()
	        {
	            return this.replace(/^\s*/, "").replace(/\s*$/, "");
	        },
	        enumerable: false
	    });
	}
	
	if(!String.prototype.stripNonAlpha)
	{
	    Object.defineProperty(String.prototype, 'stripNonAlpha',
	    {
	        value: function()
	        {
	            return this.replace(/[^A-Za-z ]+/g, "");
	        },
	        enumerable: false
	    });
	}
	
	if(!Object.prototype.sizeof)
	{
	    Object.defineProperty(Object.prototype, 'sizeof',
	    {
	        value: function()
	        {
	            var counter = 0;
	            for(index in this) counter++;
	            
	            return counter;
	        },
	        enumerable: false
	    });
	}
}



</script>



</html>